package com.nuo.test.searchInList;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class SearchInList {
	public static void main(String[] args) {
		List list = new ArrayList();
		list.add(59);
		list.add(79);
		list.add(4);
		list.add(42);
		list.add(89);
		list.add(29);
		
		System.out.println(list);
		Collections.sort(list);
		System.out.println(list);
		int index = Collections.binarySearch(list, 59);
		System.out.println(index);
		
		
		List objList = new ArrayList();
		long time = System.currentTimeMillis();
		SortObject obj = new SortObject(56, "2b1", new Date(time));
		SortObject obj2 = new SortObject(56, "2b1", new Date(time));
		System.out.println(obj.hashCode());
		System.out.println(obj2.hashCode());
		System.out.println(obj2.equals(obj2));
		
		objList.add(new SortObject(58, "2b3", new Date(System.currentTimeMillis() + 200)));
		objList.add(new SortObject(78, "2b4", new Date(System.currentTimeMillis() + 500)));
		objList.add(obj);
		objList.add(new SortObject(34, "2b2", new Date(System.currentTimeMillis() + 2152)));
		objList.add(new SortObject(2, "2b5", new Date(System.currentTimeMillis() + 6000)));
		System.out.println(objList);
		Collections.sort(objList);
		System.out.println(objList);
		index = Collections.binarySearch(objList, obj2);
		System.out.println(index);
		
	}
}


class SortObject implements Comparable<SortObject>{
	Integer userId;
	String state;
	Date logintime;
	
	public SortObject(){}
	
	public SortObject(Integer userId, String state, Date logintime){
		this.userId = userId;
		this.state = state;
		this.logintime = logintime;
	}
	
	
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public Date getLogintime() {
		return logintime;
	}
	public void setLogintime(Date logintime) {
		this.logintime = logintime;
	}
	
	public int compareTo(SortObject obj) {
		return this.getLogintime().compareTo(obj.getLogintime());
	}
	@Override
	public String toString() {
		return "SortObject [userId=" + userId + ", state=" + state
				+ ", logintime=" + logintime + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((state == null) ? 0 : state.hashCode());
		result = prime * result + ((userId == null) ? 0 : userId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		return false;
	}
}
